package assets
{
	import away3d.arcane;
	use namespace arcane;
	
	import events.LoadedEvent;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.System;
	
	import tool.DataParser;
	
	/**
	 * 3D模型的资源管理器，有staticMesh与skinnedMesh两种资源通过它取得。
	 */	
	public class AssetMesh extends EventDispatcher
	{
		////////////////////////////////////////////////////////
		private var staticMeshLoadingList:Array = new Array;
		private var staticMeshURL:String;
		private var staticMeshLoader:URLLoader;
		
		private var skinnedMeshLoadingList:Array = new Array;
		private var skinnedMeshURL:String;
		private var skinnedMeshLoader:URLLoader;
		
		////////////////////////////////////////////////////////
		public function AssetMesh()
		{
			staticMeshLoader = new URLLoader;
			skinnedMeshLoader = new URLLoader;
			
			addEventListener(LoadedEvent.SKINMESH_COMPLETE,loadSkeletion);
			
			staticMeshLoader.addEventListener(IOErrorEvent.IO_ERROR,staticMeshError);
			skinnedMeshLoader.addEventListener(IOErrorEvent.IO_ERROR,skinnedMeshError);
		}
		
		private function staticMeshError(e:IOErrorEvent):void
		{
			var errorInfo:String ="没有此资源："+ staticMeshURL;
			throw new Error(errorInfo);
		}
		
		private function skinnedMeshError(e:IOErrorEvent):void
		{
			var errorInfo:String ="没有此资源："+ skinnedMeshURL;
			throw new Error(errorInfo);
		}
		
		public function loadStaticMesh(URL:String):void
		{
			if(staticMeshLoadingList.indexOf(URL) > -1)return;
			
			if(Asset.resStaticMeshs[URL] == undefined)
			{
				staticMeshLoadingList.push(URL);
				
				if(staticMeshLoadingList.length==1)loadNextStaticMesh();
				
			}else{
				dispatchEvent(new LoadedEvent(URL,LoadedEvent.STATICMESH_COMPLETE));
			}
		}
		
		public function loadSkinedMesh(URL:String):void
		{
			if(skinnedMeshLoadingList.indexOf(URL) > -1)return;
			
			if(Asset.resSkinnedMeshs[URL] == undefined)
			{
				skinnedMeshLoadingList.push(URL);
				
				if(skinnedMeshLoadingList.length==1)loadNextSkinnedMesh();
				
			}else{
				dispatchEvent(new LoadedEvent(URL,LoadedEvent.SKINMESH_COMPLETE));
			}
		}
		
		////////////////////////////////////////////////////////
		private function loadNextSkinnedMesh():void
		{
			skinnedMeshURL = skinnedMeshLoadingList[0];
			skinnedMeshLoader.addEventListener(Event.COMPLETE,onSkinnedMeshComplete);
			skinnedMeshLoader.load(new URLRequest(skinnedMeshURL));
		}
		
		private function onSkinnedMeshComplete(e:Event):void
		{
			var ld:URLLoader = e.currentTarget as URLLoader;
			
			var _xml:XML = new XML(ld.data);
			var data:Object = DataParser.getSkinedMeshFromData(_xml,null);
			var skeletonURL:String = Global.assetPath+_xml.@skeleton;
			Asset.resSkinnedMeshs[skinnedMeshURL] = data;
				
			System.disposeXML(_xml);
			
			skinnedMeshLoadingList.splice(0,1);
			dispatchEvent(new LoadedEvent(skinnedMeshURL,LoadedEvent.SKINMESH_COMPLETE));
			
			//-----------------------------
			if(skinnedMeshLoadingList.length == 0)
			{
				skinnedMeshLoader.removeEventListener(Event.COMPLETE,onSkinnedMeshComplete);
				return;
			}
			
			loadNextSkinnedMesh();
		}
		
		private function loadSkeletion(e:LoadedEvent):void
		{
			Asset.skeleton.load(Asset.resSkinnedMeshs[e.URL].skeletonURL);
		}
		
		/////////////////////
		private function loadNextStaticMesh():void
		{
			staticMeshURL = staticMeshLoadingList[0];
			staticMeshLoader.addEventListener(Event.COMPLETE,onStaticMeshComplete);
			staticMeshLoader.load(new URLRequest(staticMeshURL));
		}
		
		private function onStaticMeshComplete(e:Event):void
		{
			staticMeshLoader.removeEventListener(Event.COMPLETE,onStaticMeshComplete);
			
			var _xml:XML = new XML(staticMeshLoader.data);
			
			Asset.resStaticMeshs[staticMeshURL] = DataParser.getStaticMeshFromData(_xml);
			
			System.disposeXML(_xml);
			staticMeshLoader.close();
				
			//-----------------------------
			staticMeshLoadingList.splice(0,1);
			dispatchEvent(new LoadedEvent(staticMeshURL,LoadedEvent.STATICMESH_COMPLETE));
			
			if(staticMeshLoadingList.length == 0)
			{
				staticMeshLoader.removeEventListener(Event.COMPLETE,onStaticMeshComplete);
				return;
			}
			
			loadNextStaticMesh();
		}
		
	}
}